/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MWmi_Win32_OperatingSystem.h"

//---------------------------------------------------------------------------

MWmi_Win32_OperatingSystem::MWmi_Win32_OperatingSystem(void)
{
	propertyCount = 23;
	Initialize();
}

MWmi_Win32_OperatingSystem::~MWmi_Win32_OperatingSystem(void)
{
	DeleteChild();
}

//---------------------------------------------------------------------------

MWmiInfo* MWmi_Win32_OperatingSystem::CreateChild(void)
{
	MWmi_Win32_OperatingSystem *elem;

	try{
		elem = new MWmi_Win32_OperatingSystem;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

void MWmi_Win32_OperatingSystem::DeleteChild(void)
{
	MWmi_Win32_OperatingSystem *elem;
	
	if(next!=NULL){
		elem = (MWmi_Win32_OperatingSystem*)next;
		next = NULL;
		delete elem;
	}
}

//---------------------------------------------------------------------------

void MWmi_Win32_OperatingSystem::Initialize(void)
{
	xBootDevice              = _T("");
	xBuildNumber             = _T("");
	xBuildType               = _T("");
	xCaption                 = _T("");
	xCSName                  = _T("");
	xCurrentTimeZone         = 0;
	xInstallDate             = _T("");
	xLastBootUpTime          = _T("");
	xOrganization            = _T("");
	xOSProductSuite          = 0;
	xOSType                  = 0;
	xOtherTypeDescription    = _T("");
	xProductType             = 0;
	xRegisteredUser          = _T("");
	xSerialNumber            = _T("");
	xServicePackMajorVersion = 0;
	xServicePackMinorVersion = 0;
	xSystemDevice            = _T("");
	xSystemDirectory         = _T("");
	xSystemDrive             = _T("");
	xTotalVisibleMemorySize  = _T("");
	xVersion                 = _T("");
	xWindowsDirectory        = _T("");
}

//---------------------------------------------------------------------------

CString MWmi_Win32_OperatingSystem::GetQueryString(void)
{
	CString str;

	str = _T("select * from Win32_OperatingSystem");
	return str;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_OperatingSystem::GetPropertyName(int idxIn)
{
	CString str;

	switch(idxIn){
		case 0 :
			str = _T("BootDevice");
			break;
		case 1 :
			str = _T("BuildNumber");
			break;
		case 2 :
			str = _T("BuildType");
			break;
		case 3 :
			str = _T("Caption");
			break;
		case 4 :
			str = _T("CSName");
			break;
		case 5 :
			str = _T("CurrentTimeZone");
			break;
		case 6 :
			str = _T("InstallDate");
			break;
		case 7 :
			str = _T("LastBootUpTime");
			break;
		case 8 :
			str = _T("Organization");
			break;
		case 9 :
			str = _T("OSProductSuite");
			break;
		case 10 :
			str = _T("OSType");
			break;
		case 11 :
			str = _T("OtherTypeDescription");
			break;
		case 12 :
			str = _T("ProductType");
			break;
		case 13 :
			str = _T("RegisteredUser");
			break;
		case 14 :
			str = _T("SerialNumber");
			break;
		case 15 :
			str = _T("ServicePackMajorVersion");
			break;
		case 16 :
			str = _T("ServicePackMinorVersion");
			break;
		case 17 :
			str = _T("SystemDevice");
			break;
		case 18 :
			str = _T("SystemDirectory");
			break;
		case 19 :
			str = _T("SystemDrive");
			break;
		case 20 :
			str = _T("TotalVisibleMemorySize");
			break;
		case 21 :
			str = _T("Version");
			break;
		case 22 :
			str = _T("WindowsDirectory");
			break;

		default :
			str = _T("");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_OperatingSystem::SetPropertyValue(int idxIn, VARIANT *vtIn)
{
	if(vtIn==NULL) return true;
	if(vtIn->vt==VT_NULL) return true;

	switch(idxIn){
		case 0 :
			if(vtIn->vt!=VT_BSTR) return false;
			xBootDevice = vtIn->bstrVal;
			break;
		case 1 :
			if(vtIn->vt!=VT_BSTR) return false;
			xBuildNumber = vtIn->bstrVal;
			break;
		case 2 :
			if(vtIn->vt!=VT_BSTR) return false;
			xBuildType = vtIn->bstrVal;
			break;
		case 3 :
			if(vtIn->vt!=VT_BSTR) return false;
			xCaption = vtIn->bstrVal;
			break;
		case 4 :
			if(vtIn->vt!=VT_BSTR) return false;
			xCSName = vtIn->bstrVal;
			break;
		case 5 :
			if(vtIn->vt!=VT_I2) return false;
			xCurrentTimeZone = vtIn->iVal;
			break;
		case 6 :
			if(vtIn->vt!=VT_BSTR) return false;
			xInstallDate = vtIn->bstrVal;
			break;
		case 7 :
			if(vtIn->vt!=VT_BSTR) return false;
			xLastBootUpTime = vtIn->bstrVal;
			break;
		case 8 :
			if(vtIn->vt!=VT_BSTR) return false;
			xOrganization = vtIn->bstrVal;
			break;
		case 9 :
			if(vtIn->vt!=VT_I4) return false;
			xOSProductSuite = vtIn->lVal;
			break;
		case 10 :
			if(vtIn->vt!=VT_I4) return false;
			xOSType = (unsigned short)vtIn->lVal;
			break;
		case 11 :
			if(vtIn->vt!=VT_BSTR) return false;
			xOtherTypeDescription = vtIn->bstrVal;
			break;
		case 12 :
			if(vtIn->vt!=VT_I4) return false;
			xProductType = vtIn->lVal;
			break;
		case 13 :
			if(vtIn->vt!=VT_BSTR) return false;
			xRegisteredUser = vtIn->bstrVal;
			break;
		case 14 :
			if(vtIn->vt!=VT_BSTR) return false;
			xSerialNumber = vtIn->bstrVal;
			break;
		case 15 :
			if(vtIn->vt!=VT_I4) return false;
			xServicePackMajorVersion = (unsigned short)vtIn->lVal;
			break;
		case 16 :
			if(vtIn->vt!=VT_I4) return false;
			xServicePackMinorVersion = (unsigned short)vtIn->lVal;
			break;
		case 17 :
			if(vtIn->vt!=VT_BSTR) return false;
			xSystemDevice = vtIn->bstrVal;
			break;
		case 18 :
			if(vtIn->vt!=VT_BSTR) return false;
			xSystemDirectory = vtIn->bstrVal;
			break;
		case 19 :
			if(vtIn->vt!=VT_BSTR) return false;
			xSystemDrive = vtIn->bstrVal;
			break;
		case 20 :
			if(vtIn->vt!=VT_BSTR) return false;
			xTotalVisibleMemorySize = vtIn->bstrVal;
			break;
		case 21 :
			if(vtIn->vt!=VT_BSTR) return false;
			xVersion = vtIn->bstrVal;
			break;
		case 22 :
			if(vtIn->vt!=VT_BSTR) return false;
			xWindowsDirectory = vtIn->bstrVal;
			break;

		default :
			return false;
			break;
	}

	return true;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_OperatingSystem::SetFromPointer(MWmiInfo* wmiInfoIn)
{
	MWmi_Win32_OperatingSystem *elem;
	MWmi_Win32_OperatingSystem *elemNew;

	DeleteChild();
	Initialize();
	if(wmiInfoIn==NULL) return true;

	elem = (MWmi_Win32_OperatingSystem*)wmiInfoIn;

	xBootDevice              = elem->xBootDevice;
	xBuildNumber             = elem->xBuildNumber;
	xBuildType               = elem->xBuildType;
	xCaption                 = elem->xCaption;
	xCSName                  = elem->xCSName;
	xCurrentTimeZone         = elem->xCurrentTimeZone;
	xInstallDate             = elem->xInstallDate;
	xLastBootUpTime          = elem->xLastBootUpTime;
	xOrganization            = elem->xOrganization;
	xOSProductSuite          = elem->xOSProductSuite;
	xOSType                  = elem->xOSType;
	xOtherTypeDescription    = elem->xOtherTypeDescription;
	xProductType             = elem->xProductType;
	xRegisteredUser          = elem->xRegisteredUser;
	xSerialNumber            = elem->xSerialNumber;
	xServicePackMajorVersion = elem->xServicePackMajorVersion;
	xServicePackMinorVersion = elem->xServicePackMinorVersion;
	xSystemDevice            = elem->xSystemDevice;
	xSystemDirectory         = elem->xSystemDirectory;
	xSystemDrive             = elem->xSystemDrive;
	xTotalVisibleMemorySize  = elem->xTotalVisibleMemorySize;
	xVersion                 = elem->xVersion;
	xWindowsDirectory        = elem->xWindowsDirectory;

	if(elem->next==NULL) return true;

	try{
		elemNew = new MWmi_Win32_OperatingSystem;
	}
	catch(...){
		elemNew = NULL;
	}
	if(elemNew==NULL) return false;

	next = elemNew;

	return elemNew->SetFromPointer(elem->next);
}

//---------------------------------------------------------------------------

CString MWmi_Win32_OperatingSystem::GetFormatedName(void)
{
	CString str, sss;

	str = xCaption;

	if(!xOtherTypeDescription.IsEmpty()){
		str += _T(" ");
		str += xOtherTypeDescription;
	}

	if(xServicePackMajorVersion!=0 || xServicePackMinorVersion!=0){
		if(xServicePackMinorVersion!=0)
			sss.Format(_T(" SP %d.%d"), xServicePackMajorVersion, xServicePackMinorVersion);
		else
			sss.Format(_T(" SP %d"), xServicePackMajorVersion);
		str += sss;
	}

	if(!xVersion.IsEmpty()){
		str += _T(" (ver.");
		str += xVersion;
		str += _T(")");
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_OperatingSystem::IsEmpty(void)
{
	return xCaption.IsEmpty() ? true : false;
}

//---------------------------------------------------------------------------
